﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public partial class view : System.Web.UI.Page
{
    
    DataTable myTable;
    dbConn db = new dbConn(); string mysql;
    
    SqlConnection myConn;
    protected void Page_Load(object sender, EventArgs e)
    {
        myViewlist();
        //Verify if the page is to be displayed in Excel.

        

       

    }

    private void myViewlist()
    {
        mysql = "SELECT FirstName,LastName,Email,TimeStamp FROM nursing_supplemental_application Order by TimeStamp desc";
        //mysql = "SELECT FirstName,LastName,Email,DateTime FROM Campaign_Tracking";
       gv.DataSource = db.CallViews(mysql);
       gv.DataBind();
        
    }







    // Must  add this Function otherwise it is not populate the pages
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       // For Paging 
        gv.PageIndex = e.NewPageIndex;
        //Bind GV
        myViewlist();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        myTable = new DataTable();
        mysql = "SELECT FirstName,LastName,Email,ContactPhone ,TimeStamp FROM nursing_supplemental_application Order by TimeStamp desc";
        //mysql = "SELECT Name = LastName + ' ,' +FirstName ,Email, ";
        //mysql += "ContactPhone ,Address = [Address1]  + ' ' +[Address2]";
        //mysql += ",[State] ,[City]  ,[Zip] ,[Interview1]  ,[Interview2],[Interview3]";
        //mysql += " FROM nursing_supplemental_application Order By Name";
        using (myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["prod"].ConnectionString))
        {

            myConn.Open();
            SqlDataAdapter da = new SqlDataAdapter(mysql, myConn);
            da.Fill(myTable);
            //Export1(myTable, "NursingReport.xls");

            string attachment = "attachment; filename=NursingReport.xls";

            Response.ClearContent();

            Response.AddHeader("content-disposition", attachment);

            Response.ContentType = "application/vnd.ms-excel";

            string tab = "";

            foreach (DataColumn dc in myTable.Columns)
            {

                Response.Write(tab + dc.ColumnName);

                tab = "\t";

            }

            Response.Write("\n");



            int i;

            foreach (DataRow dr in myTable.Rows)
            {

                tab = "";

                for (i = 0; i < myTable.Columns.Count; i++)
                {

                    Response.Write(tab + dr[i].ToString());

                    tab = "\t";

                }

                Response.Write("\n");

            }

            Response.End();






        }
        
    }
}
